package com.fm.financemanage.service;

import com.fm.financemanage.exception.BusinessException;
import com.fm.financemanage.mapper.AccountantMapper;
import com.fm.financemanage.mapper.BusinessEquipmentMapper;
import com.fm.financemanage.model.po.Accountant;
import com.fm.financemanage.model.po.BusinessEquipment;
import com.fm.financemanage.model.po.SimCard;
import com.fm.financemanage.model.vo.PageVo;
import com.fm.financemanage.model.vo.equipment.BusinessEquipmentListVo;
import com.fm.financemanage.model.vo.equipment.BusinessEquipmentOperatorVo;
import com.fm.financemanage.model.vo.simcard.SimCardListVo;
import com.fm.financemanage.model.vo.simcard.SimCardOpeartorVo;
import com.fm.financemanage.utils.TextUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class EquipmentService {

    @Resource
    private BusinessEquipmentMapper businessEquipmentMapper;
    @Resource
    private AccountantMapper accountantMapper;


    public PageVo<List<BusinessEquipmentListVo>> getList(Long userId, String number, Integer accountantId, Integer onlineState, Integer page, Integer limit) {

        return new PageVo<List<BusinessEquipmentListVo>>(businessEquipmentMapper.getList(userId,null, number, accountantId, onlineState, (page - 1) * limit, limit),
                businessEquipmentMapper.count(userId, null,number, accountantId, onlineState));

    }


    public void operator(BusinessEquipmentOperatorVo businessEquipmentOperatorVo) {
        final BusinessEquipment businessEquipment = new BusinessEquipment();
        BeanUtils.copyProperties(businessEquipmentOperatorVo, businessEquipment);
        if (businessEquipment.getId() == null) {
            businessEquipment.setUuid(UUID.randomUUID().toString().replace("-", ""));
            businessEquipment.setCreateTime(LocalDateTime.now());
            businessEquipment.setUpdateTime(LocalDateTime.now());
            businessEquipmentMapper.insertSelective(businessEquipment);
        } else {
            businessEquipment.setUpdateTime(LocalDateTime.now());
            businessEquipmentMapper.updateByPrimaryKeySelective(businessEquipment);
        }
    }


    public void removeById(String uuid) {
        businessEquipmentMapper.deleteByUuid(uuid);
    }

    // 设备绑定会计
    public void bindAccountant(String accountantUuids, String equUuid, Long userId) {
        if (TextUtil.isEmpty(accountantUuids)) {
            // 取消所有的当前设备绑定的会计员
            accountantMapper.cancelEquId(equUuid);
            return;
        }
        final String[] split = accountantUuids.split(",");
        // 判断当前会计员是否属于当前代账用户
        final List<String> uuids = Arrays.asList(split);
        List<Long> userIds = accountantMapper.getUserIdByUuids(uuids);
        if (userIds.size() > 1 || !userIds.get(0).equals(userId)) {
            throw new BusinessException("绑定的会计员不存在");
        }
        // 取消所有的当前设备绑定的会计员
        accountantMapper.cancelEquId(equUuid);
        accountantMapper.batchUpdateEquId(equUuid, uuids);
    }

}
